#!/bin/sh

if [ "$SYNOPKG_PKG_STATUS" = "INSTALL" ]; then
    AIRCONNECT_CONFIG_FILE="airconnect.conf"
    AIRCONNECT_CONFIG="${SYNOPKG_PKGDEST}/${AIRCONNECT_CONFIG_FILE}"
    AIRCONNECT_USER="$(echo "${SYNOPKG_PKGNAME}" | awk '{print tolower($_)}')"
    LOG="${SYNOPKG_PKGDEST}/log/${AIRCONNECT_USER}.log"

    echo "[$(date +'%T')] #### Start postinst... ####" >>"${LOG}"

    echo "[$(date +'%T')] Creating airconnect config file at \"${AIRCONNECT_CONFIG}\"" >>"$LOG"
    touch "${AIRCONNECT_CONFIG}"

    # shellcheck disable=SC2154
    if [ "${pkgwizard_binaries_aircast}" = "true" ]; then
        {
            echo "[$(date +'%T')] Using installation type for aircast only"
            echo "[$(date +'%T')] Setting AIRCAST_ENABLED=1 in \"${AIRCONNECT_CONFIG}\""
            echo "[$(date +'%T')] Setting AIRCAST_LATENCY=\"50:500\" in \"${AIRCONNECT_CONFIG}\""
            echo "[$(date +'%T')] Setting AIRCAST_LOGLEVEL=\"all=info\" in \"${AIRCONNECT_CONFIG}\""
        } >>"$LOG"
        {
            echo "AIRCAST_ENABLED=1"
            echo "AIRCAST_LATENCY=\"50:500\""
            echo "AIRCAST_LOGLEVEL=\"all=info\""
            echo "AIRUPNP_ENABLED=0"
            echo "AIRUPNP_LATENCY="
            echo "AIRUPNP_CONTENTLENGTH_MODE="
            echo "AIRUPNP_LOGLEVEL="
            echo "AIRUPNP_PORT="
        } >>"${AIRCONNECT_CONFIG}"
    elif [ "${pkgwizard_binaries_airupnp}" = "true" ]; then
        {
            echo "[$(date +'%T')] Using installation type for airupnp only"
            echo "[$(date +'%T')] Setting AIRUPNP_ENABLED=1 in \"${AIRCONNECT_CONFIG}\""
            echo "[$(date +'%T')] Setting AIRUPNP_LATENCY=\"50:500\" in \"${AIRCONNECT_CONFIG}\""
            echo "[$(date +'%T')] Setting AIRUPNP_CONTENTLENGTH_MODE=0 in \"${AIRCONNECT_CONFIG}\""
            echo "[$(date +'%T')] Setting AIRUPNP_LOGLEVEL=\"all=info\" in \"${AIRCONNECT_CONFIG}\""
            echo "[$(date +'%T')] Setting AIRUPNP_PORT=${pkgwizard_airupnp_port} in \"${AIRCONNECT_CONFIG}\""
            echo "[$(date +'%T')] Setting FILTER_AIRPLAY2_DEVICES=\"<NULL>,S1,S3,S5,S9,S12,ZP80,ZP90,S15,ZP100,ZP120,1.0,LibreWireless,Fitzwilliam,2.2.6,AllShare1.0\" in \"${AIRCONNECT_CONFIG}\""
        } >>"$LOG"
        {
            echo "AIRCAST_ENABLED=0"
            echo "AIRCAST_LATENCY="
            echo "AIRCAST_LOGLEVEL="
            echo "AIRUPNP_ENABLED=1"
            echo "AIRUPNP_LATENCY=\"50:500\""
            echo "AIRUPNP_CONTENTLENGTH_MODE=0"
            echo "AIRUPNP_LOGLEVEL=\"all=info\""
            echo "AIRUPNP_PORT=${pkgwizard_airupnp_port}"
            echo "FILTER_AIRPLAY2_DEVICES=\"<NULL>,S1,S3,S5,S9,S12,ZP80,ZP90,S15,ZP100,ZP120,1.0,LibreWireless,Fitzwilliam,2.2.6,AllShare1.0\""
        } >>"${AIRCONNECT_CONFIG}"
    else
        {
            echo "[$(date +'%T')] Using installation type for both binaries (airupnp and aircast)"
            echo "[$(date +'%T')] Setting AIRUPNP_ENABLED=1 and AIRCAST_ENABLED=1 in \"${AIRCONNECT_CONFIG}\""
            echo "[$(date +'%T')] Setting AIRCAST_LATENCY=\"50:500\" in \"${AIRCONNECT_CONFIG}\""
            echo "[$(date +'%T')] Setting AIRCAST_LOGLEVEL=\"all=info\" in \"${AIRCONNECT_CONFIG}\""
            echo "[$(date +'%T')] Setting AIRUPNP_LATENCY=\"50:500\" in \"${AIRCONNECT_CONFIG}\""
            echo "[$(date +'%T')] Setting AIRUPNP_CONTENTLENGTH_MODE=0 in \"${AIRCONNECT_CONFIG}\""
            echo "[$(date +'%T')] Setting AIRUPNP_LOGLEVEL=\"all=info\" in \"${AIRCONNECT_CONFIG}\""
            echo "[$(date +'%T')] Setting AIRUPNP_PORT=${pkgwizard_airupnp_port} in \"${AIRCONNECT_CONFIG}\""
            echo "[$(date +'%T')] Setting FILTER_AIRPLAY2_DEVICES=\"<NULL>,S1,S3,S5,S9,S12,ZP80,ZP90,S15,ZP100,ZP120,1.0,LibreWireless,Fitzwilliam,2.2.6,AllShare1.0\" in \"${AIRCONNECT_CONFIG}\""
        } >>"$LOG"
        {
            echo "AIRCAST_ENABLED=1"
            echo "AIRCAST_LATENCY=\"50:500\""
            echo "AIRCAST_LOGLEVEL=\"all=info\""
            echo "AIRUPNP_ENABLED=1"
            echo "AIRUPNP_LATENCY=\"50:500\""
            echo "AIRUPNP_CONTENTLENGTH_MODE=0"
            echo "AIRUPNP_LOGLEVEL=\"all=info\""
            echo "AIRUPNP_PORT=${pkgwizard_airupnp_port}"
            echo "FILTER_AIRPLAY2_DEVICES=\"<NULL>,S1,S3,S5,S9,S12,ZP80,ZP90,S15,ZP100,ZP120,1.0,LibreWireless,Fitzwilliam,2.2.6,AllShare1.0\""
        } >>"${AIRCONNECT_CONFIG}"
    fi

    # Setting IP
    # shellcheck disable=SC2154
    echo "[$(date +'%T')] Setting SYNO_IP=\"${pkgwizard_ip}\" in \"${AIRCONNECT_CONFIG}\"" >>"$LOG"
    echo "SYNO_IP=\"${pkgwizard_ip}\"" >>"${AIRCONNECT_CONFIG}"

    echo "[$(date +'%T')] #### Done postinst ####" >>"${LOG}"
fi

exit 0
